package com.pxmeta.chat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pxmeta.chat.entity.ChatGroup;
import com.pxmeta.chat.entity.ChatGroupApply;
import com.pxmeta.chat.entity.vo.ChatGroupApplyDetailVO;
import com.pxmeta.chat.entity.vo.ChatGroupListVO;
import com.pxmeta.chat.entity.vo.CreateGroupVO;
import com.pxmeta.chat.mapper.ChatGroupApplyMapper;
import com.pxmeta.chat.mapper.ChatGroupMapper;
import com.pxmeta.chat.service.HuanxinService;
import com.pxmeta.chat.service.IChatGroupApplyService;
import com.pxmeta.chat.service.IChatGroupService;
import com.pxmeta.common.constant.Status;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.UserDisturb;
import com.pxmeta.user.service.IUserDisturbService;
import com.pxmeta.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class ChatGroupApplyServiceImpl extends ServiceImpl<ChatGroupApplyMapper, ChatGroupApply> implements IChatGroupApplyService {

    private final HuanxinService huanxinService;
    private final IUserService userService;
    private final IChatGroupService chatGroupService;


    /**
     * 申请加入群聊
     * @param userId 申请人id
     * @param chatGroupId
     * @param reason
     */
    @Override
    public void joinGroup(Long userId, Long chatGroupId, String reason) {
        ChatGroupApply apply = new ChatGroupApply();
        apply.setChatGroupId(chatGroupId);
        apply.setReason(reason);
        apply.setUserId(userId);
        baseMapper.insert(apply);
    }

    @Override
    public List<ChatGroupApplyDetailVO> getApplyList(Long chatGroupId,Integer page,Integer size) {
        return baseMapper.getApplyList(chatGroupId,new Page(page,size));
    }

    /**
     * 获取申请数
     * @param chatGroupId
     * @return
     */
    @Override
    public Integer getApplyCount(Long chatGroupId) {
        LambdaQueryWrapper<ChatGroupApply> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ChatGroupApply::getStatus, Status.PENDING.getValue())
                .eq(ChatGroupApply::getChatGroupId, chatGroupId);
        return baseMapper.selectCount(wrapper).intValue();
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void verify(Long id, Integer status) {
        ChatGroupApply apply = baseMapper.selectById(id);
        if (null==apply){
            throw new ServiceException("审核不存在！");
        }
        Long userId = apply.getUserId();
        User user = userService.getById(userId);
        apply.setStatus(status);
        baseMapper.updateById(apply);
        if (Status.SUCCESS.getValue().equals(status)){
            huanxinService.addUserToChatGroups(apply.getChatGroupId().toString(),user.getUsername());
//            ChatGroup chatGroup = chatGroupService.getById(apply.getChatGroupId());
//            Integer userCount = chatGroup.getUserCount();
//            userCount ++;
//            chatGroup.setUserCount(userCount);
//            chatGroupService.updateById(chatGroup);
            chatGroupService.increaseUserCount(apply.getChatGroupId().toString(),1);
        }

    }
}
